#! python3
# -*- coding: UTF-8 -*-
# @Time    : 2025/05/26
# @Author  : AndrewWay
# @version : 1.0
# @notes   ：

"""

"""
import os
import sys
import yaml
from dataclasses import dataclass
from loguru import logger
from urllib.parse import quote_plus
from pathlib import Path
import urllib3

script_path = os.path.dirname(Path(__file__).resolve())

os.chdir(script_path)

urllib3.disable_warnings()

# 日志配置

# 日志地址
LOG_FILE_PATH = './../log'

# 输出日志格式
logger_format = "{time:YYYY-MM-DD HH:mm:ss} [{thread}] {level} {file} {line} -- {message}"

# 控制台输出
logger.add(sys.stdout,
           format=logger_format,
           level="INFO")

# 日志文件输出
if not os.path.exists(LOG_FILE_PATH):
    os.makedirs(LOG_FILE_PATH)
logger.add(os.path.join(LOG_FILE_PATH, "wechat_{time:YYYYMMDD}.log"),
           format=logger_format,
           level="INFO",
           rotation="00:00",
           compression="zip",
           enqueue=True,  # 可能不需要开启，开启反而会有问题
           )


def get_config():
    with open("conf.yml", encoding="utf-8") as f:
        config = yaml.safe_load(f)
        return config


_configs = get_config()


######################## 数据库配置 ########################


@dataclass
class DatabaseConfig:
    """
    数据库配置
    """
    host: str = _configs['database']['host']
    port: int = _configs['database']['port']
    username: str = _configs['database']['username']
    password: str = _configs['database']['password']
    database: str = _configs['database']['database']
    charset: str = _configs['database']['charset']
    pool_size: int = _configs['database']['pool_size'] if 'pool_size' in _configs['database'] else 20
    max_overflow: int = _configs['database']['max_overflow'] if 'max_overflow' in _configs['database'] else 10
    pool_recycle: int = _configs['database']['pool_recycle'] if 'pool_recycle' in _configs['database'] else 3600
    pool_timeout: int = _configs['database']['pool_timeout'] if 'pool_timeout' in _configs['database'] else 10
    pool_pre_ping: bool = _configs['database']['pool_pre_ping'] if 'pool_pre_ping' in _configs['database'] else True
    SQLALCHEMY_URI = (f"mysql+pymysql://{quote_plus(username)}:{quote_plus(password)}@"
                      f"{quote_plus(host)}:{port}/{database}?charset={charset}")
